<template>
  <div class="largeScreen_main">
    <div class="largeScreen_header">
      <div class="largeScreen_header_title">今日设备状态</div>

      <div v-if="deviceIds.length>0&&tableData&&tableData.data1" class="largeScreen_header_right">
        <div class="choose_item power0">
          运行
        </div>
        <div class="choose_item power1">
          空闲
        </div>
        <div class="choose_item power2">
          关机
        </div>

      </div>
    </div>
    <div v-if="deviceIds.length>0&&tableData&&tableData.data1" class="echarts_container">
      <div v-if="tableData&&tableData.data1.boxName" class="echarts_item">
        <div class="echarts_item_left">{{ tableData.data1.boxName }}</div>
        <div class="echarts_item_right">
          <div class="echarts_item_right_top">
            <div
              class="echarts_item_right_top_webList"
              :style="{ borderColor: tableData.data1.color }"
            >
              <div
                v-for="(item, index) in tableData.data1.lineList"
                :key="index"
                class="webList_item"
                :style="{
                  marginLeft: item.marginLeft,
                  backgroundColor: item.color,
                  width: item.width?item.width:'unset',
                  flex:item.flex?1:'unset'
                }"
              />
            </div>
          </div>
          <div class="echarts_item_right_bottom" :style="{ backgroundImage: 'url(' + backgroundImage + ')' }" />
        </div>
      </div>
      <div v-if="tableData&&tableData.data2.boxName" class="echarts_item">
        <div class="echarts_item_left">{{ tableData.data2.boxName }}</div>
        <div class="echarts_item_right">
          <div class="echarts_item_right_top">
            <div
              class="echarts_item_right_top_webList"
              :style="{ borderColor: tableData.data2.color }"
            >
              <div
                v-for="(item, index) in tableData.data2.lineList"
                :key="index"
                class="webList_item"
                :style="{
                  marginLeft: item.marginLeft,
                  backgroundColor: item.color,
                  width: item.width?item.width:'unset',
                  flex:item.flex?1:'unset'
                }"
              />
            </div>
          </div>
          <div class="echarts_item_right_bottom" :style="{ backgroundImage: 'url(' + backgroundImage + ')' }" />
        </div>
      </div>
      <div v-if="tableData&&tableData.data3.boxName" class="echarts_item">
        <div class="echarts_item_left">{{ tableData.data3.boxName }}</div>
        <div class="echarts_item_right">
          <div class="echarts_item_right_top">
            <div
              class="echarts_item_right_top_webList"
              :style="{ borderColor: tableData.data3.color }"
            >
              <div
                v-for="(item, index) in tableData.data3.lineList"
                :key="index"
                class="webList_item"
                :style="{
                  marginLeft: item.marginLeft,
                  backgroundColor: item.color,
                  width: item.width?item.width:'unset',
                  flex:item.flex?1:'unset'
                }"
              />
            </div>
          </div>
          <div class="echarts_item_right_bottom" :style="{ backgroundImage: 'url(' + backgroundImage + ')' }" />
        </div>
      </div>
      <div v-if="tableData&&tableData.data4.boxName" class="echarts_item">
        <div class="echarts_item_left">{{ tableData.data4.boxName }}</div>
        <div class="echarts_item_right">
          <div class="echarts_item_right_top">
            <div
              class="echarts_item_right_top_webList"
              :style="{ borderColor: tableData.data4.color }"
            >
              <div
                v-for="(item, index) in tableData.data4.lineList"
                :key="index"
                class="webList_item"
                :style="{
                  marginLeft: item.marginLeft,
                  backgroundColor: item.color,
                  width: item.width?item.width:'unset',
                  flex:item.flex?1:'unset'
                }"
              />
            </div>
          </div>
          <div class="echarts_item_right_bottom" :style="{ backgroundImage: 'url(' + backgroundImage + ')' }" />
        </div>
      </div>
    </div>
    <div v-else class="echarts_noData">
      <svg-icon icon-class="暂无数据" />
    </div>

  </div>
</template>

<script>
import { ganttChart } from '@/api/system/largeScreen'
const backgroundImage = require('@/assets/images/equipmentManage/bgg6.png')
export default {
  props: {
    deviceIds: {}
  },
  data() {
    return {
      backgroundImage: backgroundImage,
      tableData: {
        data1: {},
        data2: {},
        data3: {},
        data4: {}
      },
      echarts1: undefined,
      echarts2: undefined,
      echarts3: undefined,
      echarts4: undefined,

      timer: undefined
    }
  },
  mounted() {
    this.getData()
    this.timer = setInterval(() => {
      this.getData()
    }, this.$store.state.common.refreshFrequency * 1000)
  },
  beforeDestroy() {
    clearInterval(this.timer)
  },
  methods: {

    handleWebList(webList) {
      const colors = ['#34E1A4', '#FF9292', '#FAC729', '#4878A6', '#FF2E2E']
      const statuses = ['RUN', 'ALARM', 'STOP', 'OFFLINE', 'EMER']
      this.webListData = {}
      const cacheData = {}
      if (webList.length > 0) {
        cacheData.lineList = webList.map((item, index) => {
          item.color = colors[statuses.indexOf(item.runningStatus)]
          if (item.endTime.split(' ')[1] == '23:59:59') {
            item.flex = 1
          } else {
            const s = item.lastTime
            item.width = `${(s / (24 * 60 * 60)).toFixed(4) * 100}%`
          }
          if (index == 0) {
            const startTime = this.time_to_sec(item.startTime.split(' ')[1])
            item.marginLeft = `${(startTime / (24 * 60 * 60)).toFixed(4) *
            100}%`
          } else {
            item.marginLeft = 0
          }
          return item
        })
      }
      cacheData.color = colors[statuses.indexOf(webList[webList.length - 1].runningStatus)]

      // this.webListData = cacheData
      return cacheData
    },
    time_to_sec(time) {
      var s = ''

      var hour = time.split(':')[0]
      var min = time.split(':')[1]
      var sec = time.split(':')[2]

      s = Number(hour * 3600) + Number(min * 60) + Number(sec)

      return s
    },
    getData() {
      ganttChart({ companyId: this.$store.state.user.companyId }).then(res => {
        const resData = this.$deepClone(res.data)
        console.log(resData)

        if (this.deviceIds && this.deviceIds.length > 0) {
          this.deviceIds.forEach((item, index) => {
            if (resData.filter(item2 => item2.companyDeviceId == item).length != 0) {
              this.tableData['data' + (index + 1)] = this.handleWebList((resData.filter(item2 => item2.companyDeviceId == item))[0].webList)
              this.tableData['data' + (index + 1)].boxName = (resData.filter(item2 => item2.companyDeviceId == item))[0].boxName
            } else {
              this.tableData['data' + (index + 1)] = {}
            }
          })

          this.$nextTick(() => {
            console.log(this.tableData)
          })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.largeScreen_main{
  width: 100%;
  height: 100%;
}
  .largeScreen_header{
    display: flex;
    justify-content: space-between;
    .largeScreen_header_title{
      font-size: 20px;
      height: 60px;display: flex;align-items: center;
      font-weight: bold;
      color: #333333;
      padding-left: 20px;
    }

    .largeScreen_header_right{
      font-size: 16px;
      padding-right: 20px;
      height: 60px;display: flex;align-items: center;

      .choose_item{
        display: flex;justify-content: center;align-items: center;
        margin-left: 40px;
        width: 60px;height: 16px;
        border-radius: 2px;
        font-size: 10px;color: #FFFFFF;
      }
      .power0{
          background-color: #34E1A4;
      }
      .power1{
          background-color: #FAC729;
      }
      .power2{
          background-color: #4878A6;
      }
    }
  }
.echarts_noData{
  width: 100%;
  height: calc(100% - 60px);
}
  .echarts_container{
    width: calc(100% - 40px);
    height: calc(100% - 70px);
    margin: 0 10px;
  }
  .echarts_item{
    width: 100%;
    height: 25%;
    margin: 0 10px;
    display: flex;
    .echarts_item_left{
      width: 80px;margin-right: 10px;
      height: 75%;
      display: flex;justify-content: flex-start;align-items: center;
      color: #8E99BA;font-size: 10px;
      overflow:hidden; //超出的文本隐藏
      text-overflow:ellipsis; //溢出用省略号显示
      white-space:nowrap; //溢出不换行
    }
    .echarts_item_right{
      width: calc(100% - 80px);
      height: 100%;
      .echarts_item_right_top{
        width: 100%;
        height: calc(100% - 30px);

        .echarts_item_right_top_webList{
          height:100%;display: flex;align-items: center;border-radius: 6px;
          border: 3px solid #FFFFFF;
          box-shadow: inset 0px 0px 4px 0px rgba(43, 98, 203, 0.24);
          background-color: #F4F9FF;;
          .webList_item{
            height: 100%;
          }
          .webList_item:first-child{
            border-radius: 4px 0 0 4px;
          }
          //.weblist_item:last-child{
          //  border-radius: 0 4px  4px 0;
          //}
        }
      }
      .echarts_item_right_bottom{
        width: 100%;
        height: 30px;

        background-size: 100% auto;
        background-repeat: no-repeat;
        background-position: center 3px;
      }
    }
  }

</style>

